package com.han.bom;
import java.util.ArrayList;

/**
 * Represents a student in the system.
 * This instance of the User object is used to take an exam currently active.
 * 
 * @author Adjell Ian Pabayos
 * 
 * @version 1.0
 * @since JDK 1.7
 * 
 * @see Exam
 * @see ExamResponse
 * @see User
 *
 */

public class Student extends User {
	private ArrayList<ExamResponse> examAnswers;
	private ArrayList<Exam> examsToTake;
	private static int totalNumStudents = 0;

	/**
	 * Sole Constructor, sets the user id, and the total number of students
	 * 
	 * @see User
	 * 
	 */
	public Student(int userID) {
		super("S"+userID);
		setTotalNumStudents(getTotalNumStudents()+1);
	}

	/**
	 * Returns the total number of students currently taking the students
	 * 
	 * @return
	 */
	public static int getTotalNumStudents() {
		return totalNumStudents;
	}

	/**
	 * Sets the total number of students who will take the exam
	 * 
	 * @param totalNumStudents - the number of students that are going to take the exam
	 */
	public static void setTotalNumStudents(int totalNumStudents) {
		Student.totalNumStudents = totalNumStudents;
	}

	/**
	 * Returns the list of answers given by the student in response to the questions
	 * 
	 * @return
	 */
	public ArrayList<ExamResponse> getExamAnswers() {
		return examAnswers;
	}

	/**
	 * Sets the list of answers to be given by the student
	 * 
	 * @param examAnswers - answers given by the students in response to the questions
	 */
	public void setExamAnswers(ArrayList<ExamResponse> examAnswers) {
		this.examAnswers = examAnswers;
	}

	/**
	 * Returns the list of exams to be taken by the student
	 * 
	 * @return
	 */
	public ArrayList<Exam> getExamsToTake() {
		return examsToTake;
	}

	/**
	 * Sets the list of exams to be taken by the student
	 * 
	 * @param examsToTake - list of exams to be taken by the student
	 */
	public void setExamsToTake(ArrayList<Exam> examsToTake) {
		this.examsToTake = examsToTake;
	}
	
	/**
	 * Searches for an exam to be taken by the student. Returns null if not found
	 * 
	 * @param exam - exam that the student is currently taking
	 * @return
	 */
	public Exam searchExam(Exam exam){
		Exam found = null;
		int index = examsToTake.indexOf(exam);
		if(index!=-1){
			found = examsToTake.get(index);
		}
		return found;
	}
	
	/**
	 * Searches for an exam using the exam's ID. Returns null if not found
	 * 
	 * @param examID - ID of the exam
	 * @return
	 */
	public Exam searchExam(String examID){
		Exam ret = null;
		
		
		for(Exam exam: this.examsToTake){
			if(exam.getExamID().equals(examID)){
				ret = exam;
				break;
			}
		}
		
		return ret;
	}
	
	/**
	 * Searches for an exam response for the exam using its ID. Returns null if not found
	 * 
	 * @param examID - ID of the exam
	 * @return
	 */
	public ExamResponse searchExamResponse(String examID){
		ExamResponse examResponse = null;
		
		for(ExamResponse er: this.examAnswers){
			if(er.getExamResponseID().equals(examID+this.getUserID())){
				examResponse = er;
				break;
			}
		}
		
		return examResponse;
	}
	
	/**
	 * Allows the students to take a given exam, and returns a list of answers given by the student in response to the questions.
	 * 
	 * @param exam - new exam to be taken by the student.
	 * @return
	 */
	public ExamResponse takeExam(Exam exam){
		Exam toTake = searchExam(exam);
		ExamResponse newER = null;
		
		if(toTake!=null){
			newER = new ExamResponse(exam, this.getUserID(), new ArrayList<Answer>());
			this.examAnswers.add(newER);
			newER = this.examAnswers.get(examAnswers.indexOf(newER));
		}
		
		return newER;
	}
	
	/**
	 * Gets the result of the exam taken by the student, returns null if there is no result yet.
	 * 
	 * @param exam - exam taken by the student
	 * @return
	 */
	public String getGrade(Exam exam){
		String grade = null;
		
		int index = examAnswers.indexOf(exam.getExamID()+this.getUserID());
		
		if(index != -1){
			ExamResponse er = examAnswers.get(index);
			String rawScore = Integer.toString(er.computeScore());
			String maxScore = Integer.toString(er.getExam().getQuestions().size());
			grade =  rawScore +" / "+ maxScore +" = "+ Double.toString(Double.parseDouble(rawScore)/Double.parseDouble(maxScore));
		}
		
		return grade;
	}
	
}
